package com.lesso.lpq.sunflower.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lpq.sunflower.core.ApiResponse;
import com.lesso.lpq.sunflower.pojo.dto.EmployeeAttendanceRuleDTO;
import com.lesso.lpq.sunflower.pojo.dto.EmployeeAttendanceRuleQueryDTO;
import com.lesso.lpq.sunflower.pojo.vo.EmployeeAttendanceRuleVO;
import com.lesso.lpq.sunflower.service.IEmployeeAttendanceRuleService;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName : DeviceController
 * @Description :
 * @Author : 罗培棋
 * @Date: 2025-07-05 08:48
 */
@RestController
@RequestMapping("/rel/employee-attendance-rule")
public class EmployeeAttendanceRuleController {

    @DubboReference
    private IEmployeeAttendanceRuleService employeeAttendanceRuleService;

    @PostMapping
    public ApiResponse<Long> save(@Valid @RequestBody EmployeeAttendanceRuleDTO dto) {
        return ApiResponse.ok("success", employeeAttendanceRuleService.save(dto));
    }

    @GetMapping("{employeeId}")
    public ApiResponse<EmployeeAttendanceRuleVO> getByEmployeeId(@PathVariable String employeeId) {
        return ApiResponse.ok("SUCCESS", employeeAttendanceRuleService.getByEmployeeId(employeeId));
    }

    @GetMapping("/page")
    public ApiResponse<Page<EmployeeAttendanceRuleVO>> page(@RequestBody EmployeeAttendanceRuleQueryDTO dto) {
        return ApiResponse.ok("success", employeeAttendanceRuleService.page(dto));
    }


    @PutMapping("/{id}")
    public ApiResponse<Object> update(@PathVariable Long id, @RequestBody EmployeeAttendanceRuleDTO dto) {
        dto.setId(id); // 确保ID传入
        employeeAttendanceRuleService.update(dto);
        return ApiResponse.ok();
    }

    @GetMapping("/custom/page")
    public ApiResponse<Page<EmployeeAttendanceRuleVO>> customPage(EmployeeAttendanceRuleQueryDTO dto) {
        return ApiResponse.ok("success", employeeAttendanceRuleService.customPage(dto));
    }

    @DeleteMapping
    public ApiResponse<Object> delete(@RequestParam("id") Long id) {
        Assert.notNull(id, "关联ID不能为空");
        if (id < 0) {
            throw new RuntimeException("关联ID不能小于0");
        }
        employeeAttendanceRuleService.deleteById(id);
        return ApiResponse.ok();
    }
}